package com.lt.websocket;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.filechooser.FileSystemView;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping(value = "/my")
public class BitcoinControler {

	@RequestMapping(value = "/bitcoin")
	public ModelAndView bitcoin(HttpServletRequest req) {
		ServletContext servletContext = req.getSession().getServletContext();
		String desktopDir = FileSystemView.getFileSystemView().getHomeDirectory().getAbsolutePath();
		System.out.println(desktopDir);
		servletContext.setAttribute("name", desktopDir);
		ModelAndView mav = new ModelAndView("index");
		return mav;
	}

	@RequestMapping(value = "/test")
	public ModelAndView test(HttpServletRequest req) {
		String desktopDir = FileSystemView.getFileSystemView().getHomeDirectory().getAbsolutePath();
		ModelAndView mav = new ModelAndView("test");
		return mav;
	}

	@RequestMapping(value = "/submit")
	public void submit(HttpServletRequest req, HttpServletResponse rsp, Model model, @RequestParam String name, String age) {
		System.out.println(model);
		System.out.println(name);
		System.out.println(age);
	}

	@RequestMapping(value = "/downFile")
	public void downFile(HttpServletRequest req, HttpServletResponse rsp) {
		try {
			File file = new File("C:\\Users\\Administrator\\Desktop\\简约2号.pptx");
			String filename = URLEncoder.encode("我的ppt.pptx", "utf-8");
			rsp.setHeader("Content-Disposition", "attachment; filename=" + filename);
			rsp.setContentLengthLong(file.length());
			String desktopDir = FileSystemView.getFileSystemView().getHomeDirectory().getAbsolutePath();

			ServletOutputStream out = rsp.getOutputStream();
			FileInputStream in = new FileInputStream(file);
			int length = -1;
			byte[] bt = new byte[1024];
			while ((length = in.read(bt)) > 0) {
				out.write(bt, 0, length);
			}
			in.close();
			out.flush();
			out.close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
